<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/2/11
 * Time: 21:54
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\Feedback;
use App\Http\Logics\BaseLogic;
class FeedbackLogic extends BaseLogic
{
    /**
     * 创建反馈
     * @param $data
     * @return mixed
     */
    public static function createFeedback($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code) {
            return self::response($response->code);
        }
        $user_id = $response->data;
        $feedback = new Feedback();
        $feedback->user_id = $user_id;
        $feedback->content = $data['content'];
        $feedback->parent_id = $data['parent_id'];
        if (!$feedback->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    /**
     * 获取反馈列表
     * @param $data
     * @return mixed
     */
    public static function getFeedbackList($data)
    {
        $query = Feedback::with(['user'])->where(function ($query) use ($data){
            if (isset($data['loginSession']) && !empty($data['loginSession'])){
                $response = self::getUserIdByLoginSession($data['loginSession']);
                if ($response->code) {
                    return self::response($response->code);
                }
                $user_id = $response->data;
                $query->where('user_id', $user_id);
            }
            if (isset($data['parent_id']) && $data['parent_id']){
                $query->where('parent_id', $data['parent_id']);
            }
        })->orderBy('id', 'desc');
        $feedback = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($feedback)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $feedback);
    }

    /**
     * 回复反馈
     * @param $data
     * @return mixed
     */
    public static function replyFeedback($data)
    {
        $feedback = new Feedback();
        $feedback->parent_id = $data['parent_id'];
        $feedback->content = $data['content'];
        if (!$feedback->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }
}